<?php

class ControllerAdmin_Lists extends ControllerAdmin_Base
{
	public function __construct($section) {		
		$this->section      = $section;
		$this->common_vars  = Registry::get("common_vars");
		$this->table_module = PRFX."lists";
	}
	
	public function init()
	{
		$this->pages = new structure($this->table_module);
		$this->pages->active_group = ($_GET["id"] ? $_GET["id"] : 1);
		$this->pages->mode = "lists";
		$this->pages->sorting = ' ORDER BY `Por`';
		$this->pages->field = array(						
			'edit'   => true,
			'por'    => true,
			'show'   => true,
			'delete' => true,			 
			'field_cfg' => array(
				array(
					'name'      => 'Название страницы',
					'fieldname' => 'name',
					'width'     => '50%',
					'link'      => true,			
					'attr'      => false
				),
				array(
					'name'      => 'Значение',
					'fieldname' => 'value',
					'width'     => '50%',
					'link'      => true,			
					'attr'      => false
				)
			),
			"aoData" => array(
				"bSortable" => array(
					"false",
					"false",
					"false",
					"false",
					"null",
					"null"
				)
			)
		);
	}
	
	public function addToTable()
	{
		if(!empty($_POST['type']))
		{			
			extract($_POST);			
			$date_update = date("Y-m-d H:i:s");					
			if ($_POST['type'] == 'add') {
				$date_create  = date("Y-m-d H:i:s");
				eval(functions::insert_sql($this->table_module, 'sql'));
				$r =& Registry::get("MDB")->query($sql);		
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
			}
			elseif($_POST['type'] == 'editt') {
				eval(functions::update_sql($this->table_module, 'sql', 'id', $_GET['pageid']));				
				$r =& Registry::get("MDB")->query($sql);		
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
			}
		}
	}
	
    public function out()
	{
		self::init();		
		self::addToTable();
		
		if(empty($_GET['type'])) {
			$this->action = '<div class="addbuttons"><a href="?m=content&id='.$_GET["id"].'&parent='.$_GET["parent"].'&type=add" class="ui_wo_buttons ui-state-default ui-corner-all"><span class="ui-icon ui-icon-circle-plus"></span>Добавить страницу</a></div>';			
			return $this->pages->FirstPage();
		}		
		else
		{
			if($_GET['type'] == 'edit')
			{							   
				$r =& Registry::get("MDB")->query("SELECT * FROM `".$this->table_module."` WHERE `ID`='".$_GET['pageid']."' LIMIT 1;");
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
				if($r->numRows()) {
					$Field = $r->fetchRow(MDB2_FETCHMODE_ASSOC);
				}
			}			
			elseif($_GET['type'] == 'add' || empty($_GET['type'])) {
				$Field["por"] = functions::incrPor($this->table_module, $this->pages->active_group);			
			}
			
			/* ID */
			if($this->pages->pInfo['auto_increment'] && empty($_GET['pageid'])) {
				$id_action = $this->pages->pInfo['auto_increment'];
			}
			elseif(!empty($_GET['pageid'])) {
				$id_action = $_GET['pageid'];
			}
			//print_r($this->common_vars);
			Registry::get("smarty")->assign(array(
				'SECTION'   => $this->section,				
				'ID_ACTION' => $id_action,
				'Field'     => $Field,
				'TABLE'     => $this->table_module
			));
			return Registry::get("smarty")->fetch(TEMPLATE_ADMIN.$this->common_vars["controller_template"], Registry::get("cache_id"));
		}		
    }
}

$controller = new ControllerAdmin_Lists($section);
$ob_admin_out = $controller->out();
$ob_admin_action = $controller->action;

?>